﻿using System;
using System.Diagnostics;
using System.Linq;
using CCWP7.Model;
using Caliburn.Micro;

namespace CCWP7.CaliburnMicro
{
    public class ProjectsViewModel : ProjectsViewModelBase
    {
        readonly IServerDataManager _serverDataManager;

        public ProjectsViewModel(IServerDataManager serverDataManager, INavigationService navigationService)
            :
            base(navigationService)
        {
            _serverDataManager = serverDataManager;
        }

        public override void RefreshItems()
        {
            Items.Clear();

            var projects = from server in _serverDataManager.Servers
                           where server.Report != null
                           from project in server.Report.Projects
                           where _filter(project)
                           select new ProjectViewModel(project);

            var array = projects.ToArray();

            Items.AddRange(array);

            Debug.WriteLine("Items.Count=" + Items.Count);

            Refresh();
        }

        Func<IProjectReport, bool> _filter = p => true;
        public Func<IProjectReport, bool> Filter
        {
            set
            {
                if (value == null)
                    _filter = p => true;
                else
                    _filter = value;

                RefreshItems();
            }
        }
    }
}
